<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Loading a CAN database</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="kvaser.gif"/></td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('page_kvadblib_example_load_database.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Loading a CAN database </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h1><a class="anchor" id="section_load_database_with_kvadbcreate"></a>
Load an existing database file with kvaDbCreate</h1>
<p>The function kvaDbCreate can be used to both create empty databases and to load pre-existing ones from file. The behaviour depends on whether a filename is provided. <br />
<b>Example</b>. Open an existing database. </p><div class="fragment"><div class="line"><a class="code" href="kva_db_lib_8h.html#a0ab6b252bb2dbf1312bd48208954e67b">KvaDbHnd</a> db_handle;</div><div class="line"><a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57">KvaDbStatus</a> status;</div><div class="line"></div><div class="line"><span class="comment">// Open a handle</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#gacbcd92e8da46f1b756c72d25ab92d240">kvaDbOpen</a>(&amp;db_handle);</div><div class="line"></div><div class="line"><span class="comment">// Load a database file</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#ga0da72bb21d2664d6443f7b3349ec9df5">kvaDbCreate</a>(db_handle, <span class="stringliteral">&quot;db_name&quot;</span>, <span class="stringliteral">&quot;database.dbc&quot;</span>);</div><div class="line"></div><div class="line"><span class="comment">// Close the handle since we&#39;re done with the database</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#ga08c85db8a33c4dcdb9d1114b72ab7c30">kvaDbClose</a>(&amp;db_handle);</div></div><!-- fragment --><p><br />
<b>Example</b>. Passing null instead of a filename on the other hand would have created a new database: </p><div class="fragment"><div class="line"><span class="comment">// Create new database</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#ga0da72bb21d2664d6443f7b3349ec9df5">kvaDbCreate</a>(db_handle, <span class="stringliteral">&quot;db_name&quot;</span>, NULL);</div></div><!-- fragment --><p><b>Example</b>. Open a database file and check for errors. </p><div class="fragment"><div class="line"><a class="code" href="kva_db_lib_8h.html#a0ab6b252bb2dbf1312bd48208954e67b">KvaDbHnd</a>        db_handle;</div><div class="line"><a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57">KvaDbStatus</a>     status;</div><div class="line"><span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>    buflen = 128;</div><div class="line"><span class="keywordtype">char</span>*           errstr;</div><div class="line"><span class="keywordtype">char</span>            filename[256];</div><div class="line"></div><div class="line">strcpy(filename, <span class="stringliteral">&quot;database.dbc&quot;</span>);</div><div class="line"></div><div class="line"><span class="comment">// Open a handle</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#gacbcd92e8da46f1b756c72d25ab92d240">kvaDbOpen</a>(&amp;db_handle);</div><div class="line"></div><div class="line"><span class="comment">// Load a database file</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#ga0da72bb21d2664d6443f7b3349ec9df5">kvaDbCreate</a>(db_handle, <span class="stringliteral">&quot;db_name&quot;</span>, filename);</div><div class="line"></div><div class="line"><span class="comment">// If kvadblib fails to open the database file, kvaDbErr_DbFileOpen is returned.</span></div><div class="line"><span class="keywordflow">if</span> (status == <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57a4c94ae42af8f8aa1a3fadc2ebcbf6305">kvaDbErr_DbFileOpen</a>)</div><div class="line">{</div><div class="line">    printf(<span class="stringliteral">&quot;Could not open the file &#39;%s&#39;.\n&quot;</span>, filename);</div><div class="line">}</div><div class="line"><span class="comment">// If parsing of the file fails, kvaDbErr_DbFileParse is returned. </span></div><div class="line"><span class="comment">// More information can then be retrieved by calling kvaDbGetLastParseError.</span></div><div class="line"><span class="keywordflow">else</span> <span class="keywordflow">if</span> (status == <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57a0fcaa7e1fa09f67719c429419afad009">kvaDbErr_DbFileParse</a>)</div><div class="line">{</div><div class="line">    printf(<span class="stringliteral">&quot;Could not parse the file &#39;%s&#39;.\n&quot;</span>, filename);</div><div class="line"></div><div class="line">    errstr = (<span class="keywordtype">char</span>*) malloc(buflen * <span class="keyword">sizeof</span>(<span class="keywordtype">char</span>));</div><div class="line">    status = <a class="code" href="group__kvadb__database.html#ga2b96a31d3ff595c861656bfd2ac43d55">kvaDbGetLastParseError</a>(errstr, &amp;buflen);</div><div class="line">    </div><div class="line">    <span class="comment">// If the array is too short, adjust it and try anew</span></div><div class="line">    <span class="keywordflow">if</span> (status == <a class="code" href="kva_db_lib_8h.html#a2f504ec74e40048f49ed23d630528d57a67e1ef7a1005c3028a988105fcc1d122">kvaDbErr_BufferSize</a>)</div><div class="line">    {       </div><div class="line">        free(errstr);</div><div class="line">        errstr = (<span class="keywordtype">char</span>*) malloc(buflen*<span class="keyword">sizeof</span>(<span class="keywordtype">char</span>));</div><div class="line">        status = <a class="code" href="group__kvadb__database.html#ga2b96a31d3ff595c861656bfd2ac43d55">kvaDbGetLastParseError</a>(errstr, &amp;buflen);</div><div class="line">    }</div><div class="line">    printf(<span class="stringliteral">&quot;%s&quot;</span>, errstr);</div><div class="line">    free(errstr);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// Close the handle since we&#39;re done with the database</span></div><div class="line">status = <a class="code" href="group__kvadb__database.html#ga08c85db8a33c4dcdb9d1114b72ab7c30">kvaDbClose</a>(&amp;db_handle);</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="page_kvadblib.html">Database API (kvaDbLib)</a></li>
    <li class="footer"> (canlib 5.38) Tue Feb 15 2022</li>
  </ul>
</div>
</body>
</html>
